<?php
class CSuggestion extends Zend_Db_Table
{
	protected $_name;
	protected $_primary;

	public function __construct()
	{
		$this->_name = R_PRE."suggestion";
		$this->_primary = "suggestion_id";
		parent::__construct();
		
	}

	public function add($data)
	{
		$id=$this->insert($data);		
		return $id;
	}

	public function change($flag, $suggestionId)
	{
		return $this->update(array('flag'=>$flag), 'suggestion_id = ' . intval($suggestionId));
	}
	
	public function getByOrderId($orderId)
	{
		return $this->fetchRow(array('orderid = ?'=>$orderId));
	}
	
	public function getAllCounts()
	{
		$db=$this->getAdapter();
		$select = $db->select();
		$select->from($this->_name,'COUNT(suggestion_id)');
		$select->where($this->_name.".content <> ''");
		return $db->fetchOne($select);
	}
	
	public function getAll($limit,$offset)
	{
		$db=$this->getAdapter();
		$select = $db->select();
		$select->from($this->_name,'*');
		$select->join(R_PRE."members",R_PRE."members.uid = ".$this->_name.".uid");
		$select->where($this->_name.".content <> ''");
		$select->order($this->_name.".suggestion_id DESC");
		$select->limit($limit, $offset);
		return $db->fetchAll($select);
	}
	
	public function getAllContent($limit,$offset)
	{
		$db=$this->getAdapter();
		$select = $db->select();
		$select->from($this->_name,'*');
		$select->where($this->_name.".content <> ''");
		$select->order($this->_name.".suggestion_id DESC");
		$select->limit($limit, $offset);
		$items = $db->fetchAll($select);
		$contentItems = array();
		foreach($items as $item){
			$contentItems[]=$item['content'];
		}
		return $contentItems;
	}
	
	public function updateReply($id,$reply)
	{
		$db = $this->getAdapter();
		$where = $db->quoteInto('suggestion_id = ?', $id);
		return $this->update(array('reply'=>$reply), $where);
	}
	
}